import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController, ModalController, PopoverController } from 'ionic-angular';
import { ApiProvider } from '../../../providers/api/api';
import { NotificationProvider } from '../../../providers/notification/notification';

/**
 * Generated class for the SpareEditPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-spare-edit',
  templateUrl: 'spare-edit.html',
})
export class SpareEditPage {
  private houses: any;
  private item: any;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    private api:ApiProvider,
    private notification:NotificationProvider,
    private viewCtrl: ViewController){
      this.item = navParams.get('item');
    }

  ionViewDidLoad() {
    let res = this.api.getList('sp_house',
      {
        where_sql: 'sp_house.house_type=? and sp_house.house_id like ?',
        where_type: 'string;string',
        where_value: "40;" + this.item.sp_catalog__house_id + "%"
      }).
      subscribe(res => {
        if (res['success']) {
          this.houses = res['data']['root'];
        } else {
          this.notification.showAlert(res['message']);
        }
        this.notification.hideLoading();
      })
  }

  dismiss() {
    this.houses.forEach(house => {
      if (house.sp_house__house_id == this.item.sp_catalog__stock_id) {
        this.item.sp_catalog__stock_code = house.sp_house__house_code;
        this.item.sp_catalog__stock_name = house.sp_house__house_name;
      }
    });
    this.item.sp_catalog__tax_money = this.item.sp_catalog__in_num * this.item.sp_catalog__tax_price || 0;  //计算金额
    this.viewCtrl.dismiss(this.item);
  }
}
